package com.roy.dlp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;


class MyTextArea extends JTextField implements DropTargetListener {
    public MyTextArea(){
        new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
    }
    public void dragEnter(DropTargetDragEvent dtde) {
    }

    public void dragOver(DropTargetDragEvent dtde) {
    }

    public void dropActionChanged(DropTargetDragEvent dtde) {
    }

    public void dragExit(DropTargetEvent dte) {
    }

    public void drop(DropTargetDropEvent dtde) {
        try {
            Transferable tr = dtde.getTransferable();

            if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                System.out.println("file cp");
                List list = (List) (dtde.getTransferable()
                        .getTransferData(DataFlavor.javaFileListFlavor));
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    File f = (File) iterator.next();
                    this.setText(f.getAbsolutePath());
                }
                dtde.dropComplete(true);
                this.updateUI();
            }else {
                dtde.rejectDrop();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (UnsupportedFlavorException ufe) {
            ufe.printStackTrace();
        }
    }


    // 变量声明 - 不进行修改
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // 变量声明结束

}
public class DlpUtil
        implements ActionListener
{
    private JFrame mainFrame;
    private JPanel mainPanel;
    private JTextField pathText;
//    private JTextField pathText;
    private File selectedFile;

    private void mainFrame()
    {
        this.mainFrame = new JFrame();
        this.mainFrame.setSize(400, 150);
        this.mainFrame.setTitle("文件加密解密");
        this.mainFrame.setDefaultCloseOperation(3);
        this.mainFrame.setResizable(false);

        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int left = (width - this.mainFrame.getSize().width) / 2;
        int top = (height - this.mainFrame.getSize().height) / 2;
        this.mainFrame.setLocation(left, top);

        this.mainPanel = new JPanel();
        this.mainPanel.setBackground(new Color(90, 90, 146));
        this.mainPanel.setLayout(null);

        this.pathText = new MyTextArea();
        this.pathText.setName("pathText");
        this.pathText.setSize(300, 25);
        this.pathText.setBounds(30, 20, 340, 38);
        JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setBounds(30, 20, 340, 38);

        scrollPane_1.setViewportView(pathText);

//        this.pathText = new JTextField();
//        this.pathText.setName("pathText");

//        this.pathText.setSize(300, 25);
//        this.pathText.setBounds(30, 20, 340, 25);

        JButton fileBtn = new JButton("请选择文件");
        fileBtn.setName("fileBtn");
        fileBtn.setBounds(30, 65, 100, 25);
        fileBtn.addActionListener(this);

        JButton encryptBtn = new JButton("加密");
        encryptBtn.setName("encryptBtn");
        encryptBtn.setBounds(150, 65, 100, 26);
        encryptBtn.addActionListener(this);

        JButton decryptBtn = new JButton("解密");
        decryptBtn.setName("decryptBtn");
        decryptBtn.setBounds(270, 65, 100, 26);
        decryptBtn.addActionListener(this);

//        this.mainPanel.add(this.pathText);
//        this.mainPanel.add(this.pathText);
        this.mainPanel.add(scrollPane_1);
        this.mainPanel.add(fileBtn);
        this.mainPanel.add(encryptBtn);
        this.mainPanel.add(decryptBtn);
        this.mainFrame.add(this.mainPanel);
        this.mainFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if ((e.getSource() instanceof JButton))
        {
            JButton btn = (JButton)e.getSource();
            if (btn.getName() == "fileBtn")
            {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser
                        .setFileSelectionMode(2);
                int showOpenDialog = fileChooser.showOpenDialog(this.mainPanel);
                this.selectedFile = fileChooser.getSelectedFile();
                if ((this.selectedFile != null) &&
                        (showOpenDialog == 0)) {
//                    this.pathText.setText(this.pathText.getText());
                    this.pathText.setText(this.selectedFile.getPath());
                }
            }
            else if (btn.getName() == "encryptBtn")
            {

                if (this.pathText.getText().length() != 0) {
                    selectedFile = new File(this.pathText.getText());
                    FileDlpUtil.encryptPath(this.selectedFile.getPath());
                } else {
                    JOptionPane.showMessageDialog(this.mainFrame,
                            "请选择加密文件", "小助手提示你",
                            1);
                }
            }
            else if (btn.getName() == "decryptBtn")
            {
                if (this.pathText.getText().length() != 0) {
                    selectedFile = new File(this.pathText.getText());
                    FileDlpUtil.decryptPath(this.selectedFile.getPath());
                } else {
                    JOptionPane.showMessageDialog(this.mainFrame,
                            "请选择解密文件", "小助手提示你",
                            1);
                }
            }
        }
    }

    public static void main(String[] args)
    {
        DlpUtil util = new DlpUtil();
        util.mainFrame();
    }
}
